﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace KstopaCommon
{
    public delegate void UIRefresh(UserType type);
    public class UserLogIn
    {
        string path;
        private string folderName;

        public string FolderName
        {
            get { return folderName; }
            set
            {
                folderName = value;
                path = folderName + "\\userconfig.xml";
            }
        }

        private UserType currentUser;

        public UserType CurrentUser
        {
            get { return currentUser; }
            set
            {
                currentUser = value;
                CurrentUserString = currentUser.ToString();
            }
        }

        private string currentUserString;

        public string CurrentUserString
        {
            get { return currentUserString; }
            set
            {
                if (currentUserString != value)
                {
                    currentUserString = value;
                    if (UIStatusChanged != null)
                    {
                        UIStatusChanged.Invoke(currentUser);
                    }
                }
            }
        }

        Dictionary<UserType, string> DictUserPassword = new Dictionary<UserType, string>();

        public UIRefresh UIStatusChanged = null;

        public UserLogIn(string foldername)
        {
            FolderName = foldername;
            path = foldername + "\\userlogin.xml";

            if (!File.Exists(path))
            {
                for (int i = 0; i < Enum.GetNames(typeof(UserType)).Length; i++)
                {
                    DictUserPassword.Add((UserType)i, "123");
                }
                SaveXmlConfig();
            }
            else
            {
                LoadXmlConfig();
            }
        }

        public void ChangePassword()
        {
            PasswordForm passform = new PasswordForm(DictUserPassword);
            passform.StartPosition = FormStartPosition.CenterParent;
            if (passform.ShowDialog() == DialogResult.OK)
            {
                DictUserPassword = passform.DictNamePassword;
                SaveXmlConfig();
            }
        }

        public void SignIn()
        {
            UserLogInForm userform = new UserLogInForm(DictUserPassword);
            userform.StartPosition = FormStartPosition.CenterParent;
            if (userform.ShowDialog() == DialogResult.OK)
            {
                CurrentUser = userform.UserName;
            }
            else
            {
                CurrentUser = UserType.None;
            }
        }

        public void SignOut()
        {
            CurrentUser = UserType.None;
        }

        public string GetPassword(UserType user)
        {
            string password;
            DictUserPassword.TryGetValue(user, out password);
            return password;
        }

        public string GetPassword(string user)
        {
            string password;
            DictUserPassword.TryGetValue((UserType)Enum.Parse(typeof(UserType), user), out password);
            return password;
        }

        private bool LoadXmlConfig()
        {
            try
            {
                DictUserPassword = new Dictionary<UserType, string>();
                DataSet dataset = XmlHelper.GetXml(path);
                string[] users = Enum.GetNames(typeof(UserType));
                for (int i = 0; i < users.Length; i++)
                {
                    DictUserPassword.Add((UserType)i, dataset.Tables[0].Rows[0][users[i]].ToString());
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool SaveXmlConfig()
        {
            try
            {
                XmlHelper xmlhelp = new XmlHelper();
                xmlhelp.CreateXmlRoot("root");
                string[] users = Enum.GetNames(typeof(UserType));
                for (int i = 0; i < users.Length; i++)
                {
                    xmlhelp.CreatXmlNode("root", users[i], GetPassword(users[i]));
                }
                xmlhelp.XmlSave(path);
                xmlhelp.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }

    public enum UserType
    {
        None,
        操作员,//初级权限
        管理员,//中级权限
        工程师 //高级权限
    }

}
